package jimiru;

import com.google.gson.Gson;
import okhttp3.*;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class Main {
    public static void main(String[] args) {
//        get();
        post();
    }
    private static Map<String, List<Bean.ResultBean.DataBean>> map = new HashMap<>();
    private static void post(){


        OkHttpClient  okHttpClient = new OkHttpClient();
        Gson gson = new Gson();
        String url = "http://cdlqzx.beimiyun.com/ms/Module/Education/StudentSignList.aspx?ClassId=30000000-0000-0000-0000-000000000073";
        String dateStart = "2019-05-25";
        String dateEnd = "2019-05-25";

        RequestBody  requestBody = new FormBody.Builder().
                addEncoded("submitDirectEventConfig","{\"config\":{\"extraParams\":{\"action\":\"read\",\"extraParams\":\"{\\\"page\\\":1,\\\"start\\\":0,\\\"limit\\\":100000}\"}}}").
                addEncoded("ctl41","20").
                addEncoded("_ctl41_state","[{\"value\":\"20\",\"text\":\"20\",\"index\":3}]").
                addEncoded("txtStartDate",dateStart).
                addEncoded("txtEndDate",dateEnd).
                addEncoded("__EVENTTARGET","ctl03").
                addEncoded("__EVENTARGUMENT","-|public|BindData").
                addEncoded("ctl03","zhangqilu").
                build();
        Request request = addHeaders().url(url).post(requestBody).build();
        //4.创建一个call对象,参数就是Request请求对象
        Call call = okHttpClient.newCall(request);
        //5.请求加入调度,重写回调方法
        call.enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                e.printStackTrace();
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                Bean bean = gson.fromJson(response.body().string(),Bean.class);
                for (Bean.ResultBean.DataBean bean1:bean.getResult().getData()){
                    map.putIfAbsent(bean1.getCardID(),new ArrayList<>());
                    map.get(bean1.getCardID()).add(bean1);
                }
                staticfy(dateStart,dateEnd);

            }
        });
    }

    private static void  staticfy(String startTime,String endTime){
        System.out.println(System.out.format("日期%s 到 %s 的签到记录\n",startTime,endTime));
        StringBuilder stringBuilder = new StringBuilder();
        map.forEach((item,value)->{
            stringBuilder.append(String.format("id为：%s 的小朋友%s：\n",item,value.get(0).getChineseName()));
            for (Bean.ResultBean.DataBean date:value){
                stringBuilder.append("课程:").append(date.getCourseName());
                String record = "准时";
                if (date.getStatus() == 1){
                    record = "迟到";
                }else if (date.getStatus() == 2){
                    record = "旷课";
                }
                stringBuilder.append("签到记录:").append(record).append(" ");
            }
            stringBuilder.append("\n");
        });
        System.out.println(stringBuilder);
    }
    private static Request.Builder addHeaders() {
        Request.Builder builder = new Request.Builder()
                //addHeader，可添加多个请求头  header，唯一，会覆盖
                .addHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8")
                .addHeader("Cookie", "EPS_SESSION_CODE=Syyh; ASP.NET_SessionId=aoosedomgoimmikex2nmxrjp")
                .addHeader("X-Ext-Net", "delta=true");
        return builder;
    }
    private static void get(){
        String url = "http://wwww.baidu.com";
        OkHttpClient okHttpClient = new OkHttpClient();
        final Request request = new Request.Builder()
                .url(url)
                .get()//默认就是GET请求，可以不写
                .build();
        Call call = okHttpClient.newCall(request);
        call.enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                System.out.println("asd");
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                System.out.println(response.body().string());
//                1是迟到 2是旷课 3是准时
            }
        });
    }
}
